#include "stdafx.h"
#include "bank.h"

CBank::CBank(std::string name, std::string description, long double balance, unsigned int uuid)
 : m_name(name)
 , m_description(description)
 , m_uuid(uuid)
{
    m_debts.clear();
    m_incomes.clear();
}

CBank::~CBank()
{
    
}

void CBank::print()
{
    std::cout << m_name << ":" << std::endl << m_description  << std::endl << std::endl;
    for(int i = 0; i < m_debts.size(); i++)
    {
        m_debts[i].print();
        std::cout << std::endl << std::endl;
    }
    for(int i = 0; i < m_incomes.size(); i++)
    {
        m_incomes[i].print();
        std::cout << std::endl << std::endl;
    }
}

void CBank::calcNewBalance()
{
    m_balance = 0;
    for(int i = 0; i < m_debts.size(); i++)
    {
        m_balance += m_debts[i].getTotal();
    }
    for(int i = 0; i < m_incomes.size(); i++)
    {
        m_balance += m_incomes[i].getTotal();
    }
}

void CBank::addNewDebt(std::string debtName, long double debt, double rate)
{
    CDebt temp(debtName, debt, rate);
    m_debts.push_back(temp);
}

void CBank::addNewIncome(std::string incomeName, long double income, double rate)
{
    CIncome temp(incomeName, income, rate);
    m_incomes.push_back(temp);
}

unsigned int CBank::maxDebt()
{
    long double maxVal = m_debts[0].getTotal();
    unsigned int maxIndex = 0;
    if(m_debts.size() > 1)
    {
        for(int i = 1; i < m_debts.size(); i++)
        {
            long double temp = m_debts[i].getTotal();
            if( temp > maxVal)
            {
                maxIndex = i;
                maxVal = temp;
            }
        }
    }
    return maxIndex;
}

unsigned int CBank::maxIncome()
{
    long double maxVal = m_incomes[0].getTotal();
    unsigned int maxIndex = 0;
    if(m_incomes.size() > 1)
    {
        for(int i = 1; i < m_incomes.size(); i++)
        {
            long double temp = m_incomes[i].getTotal();
            if( temp > maxVal)
            {
                maxIndex = i;
                maxVal = temp;
            }
        }
    }
    return maxIndex;
}

long double CBank::combineDebts()
{
    long double retVal = 0.0;
    for(int i = 0; i < m_debts.size(); i++)
    {
        retVal += m_debts[i].getTotal();
    }
    return retVal;
}

long double CBank::combineIncomes()
{
    long double retVal = 0.0;
    for(int i = 0; i < m_incomes.size(); i++)
    {
        retVal += m_incomes[i].getTotal();
    }
    return retVal;
}